package com.exam.controller;

import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.exam.exception.PasswordErrorException;
import com.exam.exception.UserNonExistentException;
import com.exam.service.AdminService;
import com.exam.service.LoginService;
import com.exam.service.StudentService;
import com.exam.service.TeacherService;

/**
 * @author 郭荣锋
 * 用户登录控制器
 */
@Controller
@RequestMapping("/Login")	
public class Login {
	
	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="loginServiceImpl")
	private LoginService loginService;
	@Resource(name="studentServiceImpl")
	private StudentService studentService;
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	@Resource(name="teacherServiceImpl")
	private TeacherService teacherService;
	
	/**
	  *  用户登录，进行身份验证
	 * @param 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/validate")
	public void validate(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		PrintWriter printWriter = response.getWriter();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		Integer status = Integer.parseInt( request.getParameter("status") );

		HttpSession session = request.getSession();
			session.setAttribute( "loginFlag","1" );

		//如果用户身份验证通过,将用户ID存入session
		try {
			loginService.validate(userName, password, status);
			if(1==status) {
				request.getSession().setAttribute("studentId",studentService.getStudentBySno(userName).getId());
			}
			if(2==status) {
				request.getSession().setAttribute("teacherId",teacherService.getTeacherByName(userName).getId());
			}
			if(3==status) {
				request.getSession().setAttribute("adminId",adminService.getAdminByName(userName).getId());
			}
			printWriter.write("{\"res\":\"1\"}");
		} catch (UserNonExistentException e1) { //用户不存在
			printWriter.write("{\"res\":\"0\"}");
			e1.printStackTrace();
		} catch(PasswordErrorException e2) {
			printWriter.write("{\"res\":\"2\"}");
			e2.printStackTrace();
		} finally {
			printWriter.close();
		}

	}
	
	/**
	 * 负责根据身份进行页面的转发
	 * @param
	 * @return
	 */
	@RequestMapping("/dispatcher")
	public String dispatcher(HttpServletRequest request,HttpServletResponse response) {
		Integer status = Integer.parseInt(request.getParameter("status"));//身份值
		String url = null;
		if(1==status) {
			url ="/StudentIndex/studentIndex";
		}
		if(2==status) {
			url = "/TeacherIndex/teacherIndex";
		}
		if(3==status) {
			url = "/AdminIndex/adminIndex";
		}
		return "forward:"+url;
	}
	
	/**
	 * 退出系统 
	 * @param
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpServletResponse response) {
		request.getSession().removeAttribute("adminId");//删除当前用户信息
		return "redirect:"+"/adminLogin.jsp";
	}
	
}
